package com.zzyl.nursing.controller;

import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.nursing.domain.HealthAssessment;
import com.zzyl.nursing.dto.HealthAssessmentDto;
import com.zzyl.nursing.service.IHealthAssessmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 健康评估Controller
 *
 * @author ruoyi
 * @date 2025-10-20
 */
@RestController
@RequestMapping("/nursing/healthAssessment" )
@Tag(name = "健康评估相关接口" )
public class HealthAssessmentController extends BaseController {
    @Autowired
    private IHealthAssessmentService healthAssessmentService;

    /**
     * 查询健康评估列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:assessment:list')" )
    @GetMapping("/list" )
    @Operation(summary = "查询健康评估列表" )
    public TableDataInfo list(HealthAssessment healthAssessment) {
        startPage();
        List<HealthAssessment> list = healthAssessmentService.selectHealthAssessmentList(healthAssessment);
        return getDataTable(list);
    }

    /**
     * 导出健康评估列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:assessment:export')" )
    @Log(title = "健康评估" , businessType = BusinessType.EXPORT)
    @Operation(summary = "导出健康评估列表" )
    @PostMapping("/export" )
    public void export(HttpServletResponse response, HealthAssessment healthAssessment) {
        List<HealthAssessment> list = healthAssessmentService.selectHealthAssessmentList(healthAssessment);
        ExcelUtil<HealthAssessment> util = new ExcelUtil<HealthAssessment>(HealthAssessment.class);
        util.exportExcel(response, list, "健康评估数据" );
    }

    /**
     * 获取健康评估详细信息
     */
    @PreAuthorize("@ss.hasPermi('nursing:assessment:query')" )
    @GetMapping("/{id}" )
    @Operation(summary = "获取健康评估详细信息" )
    public R<HealthAssessment> getInfo(@Schema(title = "健康评估ID" )
                                       @PathVariable("id" ) Long id) {
        return R.ok(healthAssessmentService.selectHealthAssessmentById(id));
    }

    /**
     * 新增健康评估
     */
    @PreAuthorize("@ss.hasPermi('nursing:assessment:add')" )
    @Log(title = "健康评估" , businessType = BusinessType.INSERT)
    @PostMapping
    @Operation(summary = "新增健康评估" )
    public AjaxResult add(@Schema(title = "健康评估实体" ) @RequestBody HealthAssessmentDto requestParam) {
        return success(healthAssessmentService.insertHealthAssessment(requestParam));
    }

    /**
     * 修改健康评估
     */
    @PreAuthorize("@ss.hasPermi('nursing:assessment:edit')" )
    @Log(title = "健康评估" , businessType = BusinessType.UPDATE)
    @PutMapping
    @Operation(summary = "修改健康评估" )
    public AjaxResult edit(@Schema(title = "健康评估实体" ) @RequestBody HealthAssessment healthAssessment) {
        return toAjax(healthAssessmentService.updateHealthAssessment(healthAssessment));
    }

    /**
     * 删除健康评估
     */
    @PreAuthorize("@ss.hasPermi('nursing:assessment:remove')" )
    @Log(title = "健康评估" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    @Operation(summary = "删除健康评估" )
    public AjaxResult remove(@Schema(title = "健康评估ID数组" ) @PathVariable Long[] ids) {
        return toAjax(healthAssessmentService.deleteHealthAssessmentByIds(ids));
    }

    @Operation(summary = "健康文档上传" )
    @PostMapping("/upload" )
    public AjaxResult uploadFile(@RequestParam("file" ) MultipartFile file, @RequestParam("idCardNo" ) String idCardNo) {
        String url = healthAssessmentService.uploadFile(file, idCardNo);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("url" , url);
        ajax.put("fileName" , url);
        // ajax.put("newFileName", url.substring(url.lastIndexOf("/")));
        ajax.put("originalFilename" , file.getOriginalFilename());
        return ajax;
    }
}
